using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Net;

namespace BSCTechChart
{
    struct Company
    {
        public string code;
        public string name;
    }
	/// <summary>
	/// Summary description for ctlMain.
	/// </summary>
	public class ctlMain : System.Windows.Forms.UserControl
    {
        private int nMarket;
        private string[] PriceBoardServices;
        private string[] MarketNames;
        private ComboBox cbMarkets;
        private ComboBox cbCompanies;
        private IContainer components;
        private List<Company> lstCompanies;
        private TextBox tbCompany;
        private WebBrowser wbBSCTechChart;
        private Label label1;
        private string sBSCService;

		public ctlMain()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call

		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.cbMarkets = new System.Windows.Forms.ComboBox();
            this.cbCompanies = new System.Windows.Forms.ComboBox();
            this.tbCompany = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // cbMarkets
            // 
            this.cbMarkets.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbMarkets.FormattingEnabled = true;
            this.cbMarkets.Location = new System.Drawing.Point(3, 20);
            this.cbMarkets.Name = "cbMarkets";
            this.cbMarkets.Size = new System.Drawing.Size(67, 21);
            this.cbMarkets.TabIndex = 2;
            this.cbMarkets.SelectedIndexChanged += new System.EventHandler(this.cbMarkets_SelectedIndexChanged);
            // 
            // cbCompanies
            // 
            this.cbCompanies.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbCompanies.FormattingEnabled = true;
            this.cbCompanies.Location = new System.Drawing.Point(3, 57);
            this.cbCompanies.Name = "cbCompanies";
            this.cbCompanies.Size = new System.Drawing.Size(67, 21);
            this.cbCompanies.Sorted = true;
            this.cbCompanies.TabIndex = 1;
            this.cbCompanies.SelectedIndexChanged += new System.EventHandler(this.cbCompanies_SelectedIndexChanged);
            // 
            // tbCompany
            // 
            this.tbCompany.Location = new System.Drawing.Point(3, 129);
            this.tbCompany.Name = "tbCompany";
            this.tbCompany.Size = new System.Drawing.Size(67, 20);
            this.tbCompany.TabIndex = 0;
            this.tbCompany.KeyUp += new System.Windows.Forms.KeyEventHandler(this.tbCompany_KeyUp);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(0, 103);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(50, 13);
            this.label1.TabIndex = 4;
            this.label1.Text = "All stock:";
            // 
            // ctlMain
            // 
            this.BackColor = System.Drawing.Color.White;
            this.Controls.Add(this.label1);
            this.Controls.Add(this.tbCompany);
            this.Controls.Add(this.cbCompanies);
            this.Controls.Add(this.cbMarkets);
            this.Name = "ctlMain";
            this.Size = new System.Drawing.Size(607, 453);
            this.Load += new System.EventHandler(this.ctlMain_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void ctlMain_Load(object sender, System.EventArgs e)
        {
            this.lstCompanies = new List<Company>();
            #region Component Designer generated code
            this.wbBSCTechChart = new System.Windows.Forms.WebBrowser();
            // 
            // wbBSCTechChart
            // 
            this.wbBSCTechChart.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.wbBSCTechChart.Location = new System.Drawing.Point(10, -220);
            this.wbBSCTechChart.MinimumSize = new System.Drawing.Size(20, 20);
            this.wbBSCTechChart.Name = "wbBSCTechChart";
            this.wbBSCTechChart.Size = new System.Drawing.Size(580, 700);
            this.wbBSCTechChart.TabIndex = 4;
            // 
            // ctlMain
            //
            this.Controls.Add(this.wbBSCTechChart);
            
            #endregion


            // Initialize config
            StreamReader sr = new StreamReader("config.txt");
            while (!sr.EndOfStream)
            {
                string t = sr.ReadLine().Normalize();
                switch (t)
                {
                    case "[nMarkets]":
                        nMarket = int.Parse(sr.ReadLine());
                        PriceBoardServices = new string[nMarket];
                        MarketNames = new string[nMarket];
                        for (int i = 0; i < nMarket; i++)
                        {
                            MarketNames[i] = sr.ReadLine().Trim();
                            PriceBoardServices[i] = sr.ReadLine().Trim();
                            cbMarkets.Items.Add(MarketNames[i]);
                        }
                        break;
                    case "[BSC Services]":
                        sBSCService = sr.ReadLine();
                        break;
                }
            }
            sr.Close();
            cbMarkets.SelectedIndex = 0;
		}

        private void cbMarkets_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Load danh sach cong ty
            cbCompanies.Items.Clear();
            lstCompanies.Clear();
            StreamReader sr = new StreamReader(cbMarkets.Text + "\\listCompanies.txt");
            while (!sr.EndOfStream)
            {
                Company comp = new Company();
                comp.code = sr.ReadLine().Trim();
                comp.name = sr.ReadLine().Trim();

                cbCompanies.Items.Add(comp.code);
                lstCompanies.Add(comp);
            }
            sr.Close();
            cbCompanies.SelectedIndex = 0;
        }

        private void cbCompanies_SelectedIndexChanged(object sender, EventArgs e)
        {
            wbBSCTechChart.Navigate(sBSCService + cbCompanies.Text);
            tbCompany.Focus();
        }

        private void tbCompany_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    wbBSCTechChart.Navigate(sBSCService + tbCompany.Text);
                    tbCompany.Focus();
                    break;
            }
        }
    }
}
